<?php

namespace distributed;

/**
 * Created by PhpStorm.
 * User: 554665488
 * Date: 2018-6-27
 * Time: 20:46
 * @description: 服务1
 * @Author: yfl
 * @QQ 554665488
 */
use Client\HttpClient;

include_once dirname(__DIR__) . '/autoLoader.php';

class CartService
{
    protected $data;
    protected $cartService;

    public function __construct($config, $data)
    {
        $cartService = new \Swoole\Websocket\Server('0.0.0.0', 9504);
        $this->cartService = $cartService;
        $cartServiceTcp = $this->cartService->addlistener('0.0.0.0', 9505, SWOOLE_SOCK_TCP);
        $cartServiceTcp->set([
            'max_request' => 3000
        ]);
        $cartService->on('receive', [$this, 'onReceive']);
        $this->cartService->set($config);
        $this->cartService->on('workerStart', [$this, 'onWorkerStart']);
        $this->cartService->on('message', [$this, 'onMessage']);
        $this->data = $data;
        $this->cartService->start();
    }

    public function onWorkerStart($server, $worker_id)
    {
        if ($worker_id == 0) {
            $http_client = new HttpClient('127.0.0.1', 9501);
            $data = $this->data;
            $data ['method'] = 'register';
            $data = json_encode($data);
            $http_client->asyncWebSocket(function ($cli) use ($data, $server) {
                $cli->push($data);
                //心跳维持
                swoole_timer_tick(3000, function ($id) use ($cli) {
                    $cli->push('', 9); //ping请求 不触发onMessage
                });
                //发送服务的状态
                swoole_timer_tick(2000, function ($id) use ($server, $cli) {
                    $serverStatusInfo = $server->stats();//系统的连接数
                    $systemLoad = sys_getloadavg();//系统负载
                    //服务器状态
                    $status = [
                        'method' => 'status',
                        'ip' => '106.14.120.16',
//                        'ip' => '127.0.0.1',
                        'port' => ['ws' => 9504, 'tcp' => 9505],
                        'load' => $systemLoad[2], //取15分钟的负载
                        'connection_num' => $serverStatusInfo['connection_num'],
                        'tasking_num' => $serverStatusInfo['tasking_num'],
                    ];
                    //发送服务器状态到服务中心
                    $cli->push(json_encode($status));
                });
            });
        }
    }

    public function onReceive($server,$fd,$reactor_id,$data)
    {
        $html = '<li main="1">
                       <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                        <p class="ti">微服务架构专题</p>
                        <p class="price">￥<b>88888</b><span>加入购物车</span></p>
                  </li>
                  <li main="2">
                         <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                         <p class="ti">微服务架构专题</p>
                        <p class="price">￥<b>88888</b><span>加入购物车</span></p>
                  </li>
                  <li main="3">
                    <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                    <p class="ti">微服务架构专题</p>
                    <p class="price">￥<b>88888</b><span>加入购物车</span></p>
                  </li>
                  <li main="4">
                    <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                    <p class="ti">微服务架构专题</p>
                    <p class="price">￥<b>8888</b><span>加入购物车</span></p>
                  </li>';

            $server->send($fd,$html);
    }

    public function onMessage($server, $frame)
    {
        $html = '<li main="1">
                       <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                        <p class="ti">微服务架构专题</p>
                        <p class="price">￥<b>88888</b><span>加入购物车</span></p>
                  </li>
                  <li main="2">
                         <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                         <p class="ti">微服务架构专题</p>
                        <p class="price">￥<b>88888</b><span>加入购物车</span></p>
                  </li>
                  <li main="3">
                    <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                    <p class="ti">微服务架构专题</p>
                    <p class="price">￥<b>88888</b><span>加入购物车</span></p>
                  </li>
                  <li main="4">
                    <img src="https://10.url.cn/qqcourse_logo_ng/ajNVdqHZLLDDBT1rwDGvUpWksL90r4zOhRibTfnZiakYn38KkzOvyDOyMe2xTLRZykN7tOqLekeZU/220?tp=webp" width="220px" height="220px">
                    <p class="ti">微服务架构专题</p>
                    <p class="price">￥<b>8888</b><span>加入购物车</span></p>
                  </li>';
//          var_dump($frame->fd);
        $server->push($frame->fd, $html);
    }
}

$data = [
    'ip' => '106.14.120.16',
//    'ip' => '127.0.0.1',
    'port' => ['ws' => 9504, 'tcp' => 9505],
//    'serverName' => 'cartService',
    'serverName' => 'GoodsService'
];
$config = [
    'worker_num' => 2,
    'package_max_length' => 1024 * 1024 * 2,
    'max_request' => 3000
];
new CartService($config, $data);